/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.androidpush.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import org.androidpush.R;
import org.androidpush.client.Constants;
import org.androidpush.client.LogUtil;
import org.androidpush.greendao.NotificationMsg;
import org.androidpush.greendao.NotificationMsgDao;
import org.androidpush.utils.ConstantUtils;
import org.androidpush.utils.DBUtils;

/**
 * Activity for displaying the notification details view.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class NotificationDetailsActivity extends Activity {

    private static final String LOGTAG = LogUtil
            .makeLogTag(NotificationDetailsActivity.class);

    private String callbackActivityPackageName;

    private String callbackActivityClassName;

    private NotificationMsgDao notificationMsgDao;

    public NotificationDetailsActivity() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml_mess_detail);
        ConstantUtils.aetActionBar(this, "通知");


        SharedPreferences sharedPrefs = this.getSharedPreferences(
                Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        callbackActivityPackageName = sharedPrefs.getString(
                Constants.CALLBACK_ACTIVITY_PACKAGE_NAME, "");
        callbackActivityClassName = sharedPrefs.getString(
                Constants.CALLBACK_ACTIVITY_CLASS_NAME, "");
        notificationMsgDao = DBUtils.getNotificationMsgDao(this);
        Intent intent = getIntent();
        if (!intent.getBooleanExtra("Activity",true)) {
            String notificationId = intent
                    .getStringExtra(Constants.NOTIFICATION_ID);
            String notificationApiKey = intent
                    .getStringExtra(Constants.NOTIFICATION_API_KEY);
            String notificationTitle = intent
                    .getStringExtra(Constants.NOTIFICATION_TITLE);
            String notificationMessage = intent
                    .getStringExtra(Constants.NOTIFICATION_MESSAGE);
            String notificationUri = intent
                    .getStringExtra(Constants.NOTIFICATION_URI);
            String notificationMsgDate = intent.getStringExtra(Constants.NOTIFICATION_DATE);
            String notificationMsgFrom = intent.getStringExtra(Constants.NOTIFICATION_FROM);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationId=" + notificationId);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationApiKey=" + notificationApiKey);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationTitle=" + notificationTitle);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationMessage=" + notificationMessage);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationUri=" + notificationUri);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationDate=" + notificationMsgDate);
            Log.d(LOGTAG, "NotificationDetailsActivity:notificationFrom=" + notificationMsgFrom);
            NotificationMsg msg = new NotificationMsg();
            msg.setMsgTitle(notificationTitle);
            msg.setMsgContent(notificationMessage);
            msg.setMsgDate(notificationMsgDate);
            msg.setMsgFrom(notificationMsgFrom);
            msg.setMsgUri(notificationUri);
            notificationMsgDao.insertOrReplace(msg);
            init(notificationTitle,notificationMessage,notificationMsgDate,notificationMsgFrom);
        } else {
            String title = intent.getStringExtra(ConstantUtils.MESS_TITLE);
            String content = intent.getStringExtra(ConstantUtils.MESS_CONTENT);
            String from = intent.getStringExtra(ConstantUtils.MESS_FROM);
            String date = intent.getStringExtra(ConstantUtils.MESS_DATE);
            Log.d("Tag",title+":"+content+":"+
            from+":"+date);
            init(title, content, date, from);
        }

    }

    private void init(String title, String content, String date, String from) {
        TextView titleTev = (TextView) findViewById(R.id.id_messdatail_title);
        TextView messageTev = (TextView) findViewById(R.id.id_messdatail_content);
        TextView dateTev = (TextView) findViewById(R.id.id_messdatail_time);
        TextView fromTev = (TextView) findViewById(R.id.id_messdatail_from);

        titleTev.setText(title);
        messageTev.setText(content);
        dateTev.setText(date);
        if(fromTev!=null){
            fromTev.setText(from);
        }
    }
}
